package huiji;

import enums.GameStatusEnum;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class HuijiJPanel extends JPanel {

    static public int width = 600;
    static public int height = 700;
    static private GameStatusEnum status = GameStatusEnum.NOT_START;//游戏状态

    static  public int intervel = 10;//定时器间隔：10毫秒
    public  static int timeCount = 0;//定时任务运行时长

    static public BaseObj baseObj = new BaseObj(ClassLoader.getSystemResource("feiji.gif"));
    //static public BaseObj startButton = new BaseObj(ClassLoader.getSystemResource("startgame.png"));
    static public HuijiFont notStartStr= new HuijiFont("单击此处开始游戏",new Color(255,0,0),24).setType(Font.PLAIN);

    @Override
    public void paint(Graphics g) {
        if(status==GameStatusEnum.NOT_START){
            Background.paintBackGroud(g);

            //g.drawImage(Background.bi,204,338,null);
            //notStartStr.paint(g);
            g.setColor(new Color(255,0,0));
            StartButton startButton = StartButton.startList.get(StartButton.index);
//            g.fillRect(startButton.getX(),startButton.getY(),startButton.getWidth(),startButton.getHeight());
            StartButton.paintStart(g);//绘画开始按钮
        }else if(status==GameStatusEnum.GAMING){
            //画背景
            Background.paintBackGroud(g);
            //画英雄机和他的子弹
            HeroPlane.hero.paintOneOfList(g);
            //画敌机
            Enemy.paintEnemy(g);


        }

//        g.drawImage(Background.bi,0,0,null);
//
        //baseObj.paint(g);
//

//
//        g.fillRect(100,100,20,22);



    }

    public void action(){

        //鼠标监听器
        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override//鼠标移动时间
            public void mouseMoved(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                if(status==GameStatusEnum.NOT_START){//游戏未开始
                    if(StartButton.startList.get(StartButton.index).touch(x,y)){
                        if(StartButton.index!=1){
                            StartButton.index = 1;
                            repaint();
                        }
                    }else{
                        if(StartButton.index!=0){
                            StartButton.index = 0;
                            repaint();
                        }
                    }
                }else if(status==GameStatusEnum.GAMING){
                    HeroPlane.hero.batchFollowMouse(x,y);

                    //baseObj.followMouse(x,y);
                }

            }

            @Override//鼠标点击事件
            public void mouseClicked(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                if(status==GameStatusEnum.NOT_START){//游戏未开始状态
                    if (e.getButton()==1&&StartButton.index==1&&StartButton.startList.get(StartButton.index).touch(x,y)){
                        status = GameStatusEnum.GAMING;
                        HeroPlane.hero.batchFollowMouse(x,y);//游戏开始让飞机跟随鼠标位置
                    }

                }else if(status==GameStatusEnum.GAMING){
                    //HeroPlane.hero.batchFollowMouse(x,y);//移动英雄机坐标

                    //baseObj.followMouse(x,y);
                }
            }
        };
        this.addMouseListener(mouseAdapter);//处理鼠标操作事件
        this.addMouseMotionListener(mouseAdapter);//处理鼠标滑动事件

        this.addKeyListener(new KeyAdapter() {
            @Override//键盘按下事件
            public void keyPressed(KeyEvent e) {
                e.getKeyCode();// 32为空格键
            }
        });

        Timer timer = new Timer();//

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timeCount+=intervel;
                if(status==GameStatusEnum.NOT_START){

                }else if(status==GameStatusEnum.GAMING){
                    //移动背景
                    Background.moveBackGroud();
                    HeroPlane.setHeroIndex();//设置集合下标，方便画飞机选择状态
                    HeroPlane.hero.fireBullet();//发射子弹,控制子弹移动，越界删除,摧毁敌机
                    //敌机行动（创建敌机、敌机移动、敌机越界删除）
                    Enemy.action();
                    //
                    repaint();//内部会去调用paint方法
                }


            }
        }, intervel, intervel);


    }


}
